<template>
  <div id="app">
    <van-tabbar v-model="active" active-color="rgb(255, 101, 0)" route>
      <van-tabbar-item
        v-for="(item, index) in navData"
        :key="index"
        :to="item.path"
        @click="changAcive"
      >
        {{ item.title }}
        <template #icon>
          <img
            :src="index == active ? item.icon.active : item.icon.inactive"
          /> </template
      ></van-tabbar-item>
    </van-tabbar>
    <keep-alive>
      <router-view v-if="$route.meta.keep"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keep"></router-view>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //路由激活下标
      active: 0,
      // 首页底部导航栏数据
      navData: [
        {
          title: "首页",
          path: "/home",
          icon: {
            inactive: require("@/assets/images/home1.png"),
            active: require("@/assets/images/Home.png"),
          },
        },
        {
          title: "分类",
          path: "/category",
          icon: {
            inactive: require("@/assets/images/classify1.png"),
            active: require("@/assets/images/Classify.png"),
          },
        },
        {
          title: "服务",
          path: "/server",
          icon: {
            inactive: require("@/assets/images/forum1.png"),
            active: require("@/assets/images/Forum.png"),
          },
        },
        {
          title: "购物车",
          path: "/shopcart",
          icon: {
            inactive: require("@/assets/images/shoppingcart1.png"),
            active: require("@/assets/images/Shoppingcart.png"),
          },
        },
        {
          title: "我的",
          path: "/my",
          icon: {
            inactive: require("@/assets/images/my1.png"),
            active: require("@/assets/images/My.png"),
          },
        },
      ],
    };
  },
  created() {
    this.active = window.localStorage.getItem("appActiveIndex");
  },
  watch: {
    $route(newval) {
      this.navData.forEach((item, index) => {
        if (item.path == newval.path) {
          this.active = index;
          window.localStorage.setItem("appActiveIndex", this.active);
        }
      });
      this.active = window.localStorage.getItem("appActiveIndex");
    },
  },
  methods: {
    changAcive() {
      window.localStorage.setItem("appActiveIndex", this.active);
    },
  },
};
</script>

<style lang="scss">
.auto-img {
  display: block;
  width: 100%;
}
::-webkit-scrollbar {
  width: 0 !important;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>
